package com.hualan.service.community;

import com.alibaba.fastjson.JSON;
import com.hualan.entity.Community;
import com.hualan.entity.Result;
import com.hualan.entity.ResultDate;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

//小区相关服务
@WebServlet(urlPatterns = {"/panghudaojiaBackend/community/communityUpdate","/panghudaojiaBackend/community/communityList","/panghudaojiaBackend/community/communityRemove"})
public class CommunityService extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String location = req.getServletPath();
        if(location.equals("/panghudaojiaBackend/community/communityUpdate")){
            int flag =Integer.parseInt(req.getParameter("addOrUpdateFlag"));
            if(flag == 1){
                //添加小区
                String sql = "insert into community values(null,?,?,?,?,?,?,?,?,1,?,0,0)";
                List<Object> list = new ArrayList<>();
                list.add(req.getParameter("communityName"));
                list.add(req.getParameter("communityAddress"));
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                list.add(LocalDateTime.now().format(formatter));
                list.add(req.getParameter("communityProvince"));
                list.add(req.getParameter("communityCity"));
                list.add(req.getParameter("communityDistrict"));
                list.add(req.getParameter("communityLatitude"));
                list.add(req.getParameter("communityLongitude"));
                list.add(req.getParameter("communityCoordinates"));
                ExecUtil.execute(sql,list,req,true);
                ExecUtil.commit();
                ExecUtil.close();
                resp.getWriter().write(JSON.toJSONString(new Result(200,"增加成功！")));
            }else if(flag == 2){
                //修改小区
                String sql = "update community set communityName = ?,communityAddress=?,communityProvince=?,communityCity=?,communityDistrict=?,communityLatitude=?,communityLongitude=?,communityCoordinates=? where communityId = ?;";
                List<Object> list = new ArrayList<>();
                list.add(req.getParameter("communityName"));
                list.add(req.getParameter("communityAddress"));
                list.add(req.getParameter("communityProvince"));
                list.add(req.getParameter("communityCity"));
                list.add(req.getParameter("communityDistrict"));
                list.add(req.getParameter("communityLatitude"));
                list.add(req.getParameter("communityLongitude"));
                list.add(req.getParameter("communityCoordinates"));
                list.add(req.getParameter("communityId"));
                ExecUtil.execute(sql,list,req,true);
                ExecUtil.commit();
                ExecUtil.close();
                resp.getWriter().write(JSON.toJSONString(new Result(200,"修改成功！")));
            }
        } else if (location.equals("/panghudaojiaBackend/community/communityRemove")) {
            //删除
            String sql = "delete from community where communityId = ?";
            List<Object> list = new ArrayList<>();
            list.add(req.getParameter("communityId"));
            ExecUtil.execute(sql,list,req,true);
            ExecUtil.commit();
            ExecUtil.close();
            resp.getWriter().write(JSON.toJSONString(new Result(200,"删除成功！")));
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String location = req.getServletPath();
        if(location.equals("/panghudaojiaBackend/community/communityList")){
            String sql = "select * from community where ";
            List<Object> list = new ArrayList<>();
            if(!Objects.equals(req.getParameter("name"), "")){
                sql += "communityName like ? and ";
                list.add("%"+req.getParameter("name")+"%");
            }
            if(!Objects.equals(req.getParameter("startTime"), "") && !Objects.equals(req.getParameter("endTime"), "")){
                sql += "createTime between ? and ? and ";
                list.add(req.getParameter("startTime"));
                list.add(req.getParameter("endTime"));
            }
            sql+="\'a\'=\'a\' ";
            ResultSet resultSet1 = (ResultSet) ExecUtil.execute(sql,list,req,false);
            Integer total = 0;
            try{
                while(resultSet1.next()){
                    total++;
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            ExecUtil.commit();
            ExecUtil.close();
            Integer pageNum = Integer.parseInt(req.getParameter("pageNum"));
            Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
            Integer start = pageSize*(pageNum-1);
            Integer end = pageSize*pageNum;
            sql += "limit "+start+","+end;
            ResultSet resultSet =(ResultSet) ExecUtil.execute(sql,list,req,false);
            ExecUtil.commit();
            List<Object> communities = new ArrayList<>();
            try{
                while (resultSet.next()){
                    EntityFactory entityFactory = new EntityFactory(Community.class);
                    Community community =(Community) entityFactory.startCombine(resultSet);
                    communities.add(community);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            ExecUtil.close();
            ExecUtil.close();
            resp.getWriter().write(JSON.toJSONString(new Result(200,new ResultDate(pageNum,pageSize,total,total/pageSize+(total%pageSize!=0?1:0),communities))));
        }
    }

}
